package com.aspose.cells;

class zvh {
   private com.aspose.cells.b.a.d.zm b;
   private zafh c;
   private zvb d;
   private zbcf e;
   private zrl f;
   private com.aspose.cells.b.a.d.zh g;
   private zane h;

   public zvh(zane var1) {
      this.h = var1;
   }

   public zvh(com.aspose.cells.b.a.zh var1) {
      this(new zane(var1));
   }

   public zvh(String var1) throws Exception {
      com.aspose.cells.b.a.d.ze var2 = com.aspose.cells.b.a.d.zd.b(var1);

      try {
         this.b(var2);
         this.d();
      } finally {
         if (var2 != null) {
            var2.a();
         }

      }

   }

   public zvh(com.aspose.cells.b.a.d.zm var1) throws Exception {
      this.b(var1);
      this.d();
   }

   private void b(com.aspose.cells.b.a.d.zm var1) throws Exception {
      assert var1 != null;

      this.b = var1;
      this.b.b(0L);
      this.c = new zafh(new com.aspose.cells.b.a.d.za(this.b, Encoding.getUnicode()));
      this.d = new zvb(this.b, this.c);
      if (!this.d.a()) {
         throw new IllegalStateException("The file seems to be corrupted.");
      } else {
         this.b();
         this.e = new zbcf(this.a(this.c.i, this.c.a(), this.c.a(), true));
         this.c();
      }
   }

   private void b() {
      byte var1 = 10;
      int var2 = Math.min(var1, this.d.d());

      for(long var3 = 0L; (var3 & 4294967295L) < (long)var2; ++var3) {
         if ((this.d.b(var3) & 4294967295L) != 0L) {
            return;
         }
      }

      throw new IllegalStateException("The FAT in the structured storage document seems to be corrupted.");
   }

   public void a(String var1) throws Exception {
      com.aspose.cells.b.a.d.ze var2 = com.aspose.cells.b.a.d.zd.a(var1);

      try {
         this.a((com.aspose.cells.b.a.d.zm)var2);
      } finally {
         if (var2 != null) {
            var2.a();
         }

      }

   }

   public void a(com.aspose.cells.b.a.d.zm var1) throws Exception {
      assert var1 != null;

      this.b = var1;
      this.b.b(512L);
      this.c = new zafh();
      this.d = new zvb(this.b, this.c);
      this.e = new zbcf();
      this.g = new com.aspose.cells.b.a.d.zh();
      this.f = new zrl();
      this.a((zane)this.h, (zrk)null);
      if (this.g.h() > 0L) {
         this.f.a(0L).k = this.a(this.g, true);
         this.f.a(0L).l = this.g.h();
         byte var2 = 0;
         int[] var3 = new int[]{var2};
         this.c.i = this.a(this.e.c(), true, var3);
         int var4 = var3[0];
         this.c.j = var4;
      } else {
         this.c.i = 4294967294L;
      }

      this.c.g = this.a(this.f.b(), true);
      this.d.b();
      this.b.b(0L);
      this.c.a(new com.aspose.cells.b.a.d.zb(this.b, Encoding.getUnicode()));
      this.b.b(this.b.h());
   }

   private void a(zane var1, zrk var2) throws Exception {
      if (var2 == null) {
         var2 = new zrk("Root Entry", true, var1.a());
         this.f.a(var2);
      }

      if (var1.b() > 0) {
         var2.f = (long)this.f.a();
      }

      for(int var3 = 0; var3 < var1.b(); ++var3) {
         String var5 = (String)com.aspose.cells.b.a.zl.a(var1.c(var3), String.class);
         Object var6 = var1.b(var3);
         zrk var4;
         if (var6 instanceof zane) {
            zane var7 = (zane)com.aspose.cells.b.a.zl.a(var6, zane.class);
            var4 = new zrk(var5, false, var7.a());
            this.f.a(var4);
            this.a(var7, var4);
         } else {
            if (!(var6 instanceof com.aspose.cells.b.a.d.zm)) {
               throw new IllegalStateException("Unknown object in memory storage.");
            }

            com.aspose.cells.b.a.d.zm var8 = (com.aspose.cells.b.a.d.zm)var6;
            var4 = new zrk(var5, 2, var8.h());
            this.f.a(var4);
            var4.k = this.a(var8, false);
         }

         if (var3 < var1.b() - 1) {
            var4.e = (long)this.f.a();
         }
      }

   }

   private long a(com.aspose.cells.b.a.d.zm var1, boolean var2, int[] var3) throws Exception {
      var3[0] = 0;
      if (var1.h() == 0L) {
         return 4294967294L;
      } else {
         boolean var4 = this.c.a(var1.h()) || var2;
         Object var5 = var4 ? this.d : this.e;
         Object var6 = var4 ? this.b : this.g;
         int var7 = var4 ? 512 : 64;
         long var8 = zbcg.b(((com.aspose.cells.b.a.d.zm)var6).i(), var4);
         var1.b(0L);
         zbom.a((com.aspose.cells.b.a.d.zm)var1, (com.aspose.cells.b.a.d.zm)var6);
         zbom.a((com.aspose.cells.b.a.d.zm)var6, var7);
         var3[0] = zbom.a(var1.h(), var7);

         for(long var10 = 1L; (var10 & 4294967295L) < (long)var3[0]; ++var10) {
            ((zbcf)var5).a((var8 & 4294967295L) + (var10 & 4294967295L));
         }

         ((zbcf)var5).a(4294967294L);
         return var8;
      }
   }

   private long a(com.aspose.cells.b.a.d.zm var1, boolean var2) throws Exception {
      byte var3 = 0;
      int[] var4 = new int[]{var3};
      long var5 = this.a(var1, var2, var4);
      int var7 = var4[0];
      return var5;
   }

   private void c() throws Exception {
      assert this.c != null : "Header must be read before.";

      assert this.d != null : "Fat must be read before.";

      this.f = new zrl();
      com.aspose.cells.b.a.d.za var1 = new com.aspose.cells.b.a.d.za(this.b, Encoding.getUnicode());
      byte var2 = 4;

      for(long var3 = this.c.g; var3 != 4294967294L; var3 = this.d.b(var3)) {
         this.b.b(zbcg.a(var3, true));

         for(int var5 = 0; var5 < var2; ++var5) {
            this.f.a(new zrk(var1));
         }
      }

   }

   private com.aspose.cells.b.a.d.zh a(long var1, int var3, int var4, boolean var5) throws Exception {
      var4 = Math.min(var3, var4);
      com.aspose.cells.b.a.d.zh var6 = new com.aspose.cells.b.a.d.zh(var4);
      var6.a((long)var4);
      boolean var7 = var5 || this.c.a((long)var3);
      if (!var7) {
         zrk var8 = this.f.a(0L);
         if (var8.k != 4294967295L && this.g == null) {
            this.g = this.a(var8.k, (int)var8.l, (int)var8.l, true);
         }

         if (this.c.a() == 0 || this.g == null) {
            var6.a(0L);
            return var6;
         }
      }

      Object var21 = var7 ? this.d : this.e;
      Object var9 = var7 ? this.b : this.g;
      int var10 = var7 ? 512 : 64;
      int var11 = 0;
      long var12 = ((com.aspose.cells.b.a.d.zm)var9).i();

      for(long var14 = var1; var14 != 4294967294L; var14 = ((zbcf)var21).b(var14)) {
         long var16 = zbcg.a(var14, var7);
         if (var12 != var16) {
            var12 = var16;
            ((com.aspose.cells.b.a.d.zm)var9).b(var16);
         }

         int var18 = var4 - var11;
         if (var18 == 0) {
            break;
         }

         int var19 = Math.min(var10, var18);
         int var20 = ((com.aspose.cells.b.a.d.zm)var9).a(var6.m(), var11, var19);
         if (var20 != var19) {
            com.aspose.cells.a.e.zf.a("The structured file is probably corrupt. Cannot read all data that was expected, but okay to continue.");
         }

         var11 += var19;
         var12 += (long)var19;
      }

      return var6;
   }

   private void d() throws Exception {
      this.a((zrk)this.f.a(0L), (zane)null);
   }

   private void a(zrk var1, zane var2) throws Exception {
      if (var1.m) {
         com.aspose.cells.a.e.zf.a("The structured storage file seems to be corrupt. This directory entry was already read, ignoring.");
      } else {
         var1.m = true;
         zrk var4;
         switch(var1.b) {
         case 0:
            return;
         case 1:
         case 5:
            zane var3 = new zane(var1.g);
            if (var1.b == 5 && this.h == null) {
               this.h = var3;
            } else {
               var2.a(var1.a, var3);
            }

            var4 = this.f.a(var1, var1.f);
            if (var4 != null) {
               this.a(var4, var3);
            }
            break;
         case 2:
            var2.a(var1.a, this.a(var1.k, (int)var1.l, (int)var1.l, false));
            break;
         case 3:
         case 4:
         default:
            throw new IllegalStateException("Unknown type of directory entry in the structured storage.");
         }

         zrk var5 = this.f.a(var1, var1.d);
         if (var5 != null) {
            this.a(var5, var2);
         }

         var4 = this.f.a(var1, var1.e);
         if (var4 != null) {
            this.a(var4, var2);
         }

      }
   }

   public zane a() {
      return this.h;
   }
}
